1
Designing for Flexibility: The Table Interface
AI020 Lesson 4
00:00

The foundation of a flexible system lies in defining a rigid internal contract or interface that objects must follow. In this context, we define a Table Interface where every cell object guarantees the existence of three specific methods: minWidth(), minHeight(), and draw(width, height).

1. The Interface Contract

By standardizing on these methods, the layout logic can calculate global row and column dimensions without knowing the internal data types of individual cells. This is a classic example of Interface-based Polymorphism.

2. TextCell Implementation

The TextCell constructor prepares raw input by splitting strings into line-by-line arrays. This shifts complexity from the rendering phase to the instantiation phase.

this.text = text.split("\n");

3. Deterministic Drawing

The draw(width, height) method ensures that every cell output is perfectly padded using a repeat() helper. This maintains vertical and horizontal alignment regardless of content length.

nameKilimanjaroheight5895Columns align via calculated minWidth()

$$\text{Padding} = \text{Width} - \text{line.length}$$

main.py
TERMINAL bash — 80x24
> Ready. Click "Run" to execute.
>